package net.akcegaraz.tipleague.client.view;

import java.util.ArrayList;

import net.akcegaraz.tipleague.client.composite.EventComposite;
import net.akcegaraz.tipleague.client.presenter.EditTicketPresenter.Display;

import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class EditTicketView extends Composite implements Display {

    Label roundName = new Label();
    Label guide = new Label();
    Label playerNameLabel = new Label();

    TextBox playerNameTextBox = new TextBox();

    ArrayList<EventComposite> events = new ArrayList<EventComposite>();

    Button saveButton = new Button("Save");
    Button listButton = new Button("List");

    public EditTicketView() {
	VerticalPanel verticalPanel = new VerticalPanel();
	initWidget(verticalPanel);

	fetchData();

	verticalPanel.add(roundName);
	verticalPanel.add(guide);

	HorizontalPanel playerNamePanel = new HorizontalPanel();
	playerNamePanel.add(playerNameLabel);
	playerNamePanel.add(playerNameTextBox);

	verticalPanel.add(playerNamePanel);

	for (EventComposite eventComposite : events) {
	    verticalPanel.add(eventComposite);
	}

	verticalPanel.add(saveButton);
	verticalPanel.add(listButton);
    }

    public Widget asWidget() {
	return this;
    }

    public void fetchData() {
	roundName.setText("TIP ROUND - 15. kolo");
	guide.setText("1 - vyhra\n2-prohra");
	playerNameLabel.setText("Jmeno");

	EventComposite eventComposite1 = new EventComposite();
	eventComposite1.getHomeTeam().setText("Banik");
	eventComposite1.getAwayTeam().setText("Sparta");
	eventComposite1.getBet().addItem("1");
	eventComposite1.getBet().addItem("2");
	EventComposite eventComposite2 = new EventComposite();
	eventComposite2.getHomeTeam().setText("Slavia");
	eventComposite2.getAwayTeam().setText("Teplice");
	eventComposite2.getBet().addItem("1");
	eventComposite2.getBet().addItem("2");

	events.add(eventComposite1);
	events.add(eventComposite2);
    }

    public ArrayList<String> getBets() {
	ArrayList<String> bets = new ArrayList<String>();
	for (EventComposite event : events) {
	    bets.add(String.valueOf(event.getBet().getSelectedIndex()));
	}
	return bets;
    }

    public HasClickHandlers getCreateButton() {
	return saveButton;
    }

    public HasClickHandlers getListButton() {
	return listButton;
    }

    public HasValue<String> getPlayerName() {
	return playerNameTextBox;
    }

}
